package com.bidevalution.business.impl;

import com.bidevalution.business.service.DynamicColumnDataSource;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionStep;
import com.bidevalution.dao.entity.TdTechnology;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.bidevalution.dao.mapper.TdTechnologyMapper;
import com.bidevalution.service.layout.ColumnHeader;
import com.qtp.core.util.OConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 技术标主页面展示打分细项
 * @author huyiqi
 * @date 2020/4/14
 */
@Service(value = "dcTechDetail")
public class DcTechDetailImpl implements DynamicColumnDataSource {



    @Override
    public List<ColumnHeader> getDynamicColumnHead(BeSectionStep step, ColumnHeader header) {
        BeSectionInfo sectionInfo = beSectionInfoMapper.getSectionInfoById(step.getSectionId());
        List<TdTechnology> techList = tdTechnologyMapper.getTechByProjId(sectionInfo.getTdProjectid());
        List<ColumnHeader> columnHeaders = new ArrayList<>();
        if(techList!=null) {
            for(TdTechnology tech:techList) {
                ColumnHeader head = new ColumnHeader(tech.getTechJSMethodID(), tech.getTechName()
                        , ColumnHeader.ColumnType.TEXT.getCode() ,"","","","0","",tech.getTechName(),"0");
                columnHeaders.add(head);
            }
        }
        return columnHeaders;
    }

    @Override
    public void getDynamicColumnData(BeSectionStep step, ColumnHeader header, List<Map<String, Object>> data, String expertId) {
        Map<String, Map<String, Object>> enterpriseScoreMapping = new HashMap<>(32);
        BeSectionInfo sectionInfo = beSectionInfoMapper.getSectionInfoById(step.getSectionId());
        List<Map<String, String>> scores = tdTechnologyMapper.getEnterpriseTechScores(sectionInfo.getTdProjectid(), expertId);
        if(scores!=null) {
            String enterpriseId = "";
            Map<String, Object> techScore = null;
            for(Map<String, String> score:scores) {
                if(StringUtils.isNotBlank(score.get("entId"))) {
                    if(!enterpriseId.equals(score.get("entId"))) {
                        if(techScore!=null) {
                            enterpriseScoreMapping.put(enterpriseId, techScore);
                        }
                        enterpriseId = score.get("entId");
                        techScore = new HashMap<>(16);
                    }
                    techScore.put(score.get("techId"), score.get("c1"));
                }
            }
            if(techScore!=null) {
                enterpriseScoreMapping.put(enterpriseId, techScore);
            }
        }
        for(Map<String, Object> entData:data) {
            String entId = OConvertUtils.getString(entData.get("enterprise_id"));
            Map<String, Object> techScore = enterpriseScoreMapping.get(entId);
            if(techScore!=null && !techScore.isEmpty()) {
                entData.putAll(techScore);
            }
        }
    }

    @Autowired(required = false)
    private BeSectionInfoMapper beSectionInfoMapper;
    @Autowired(required = false)
    private TdTechnologyMapper tdTechnologyMapper;
}
